{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 时间数据重采样"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## resample"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2017-01-01    0\n",
      "2017-01-02    1\n",
      "2017-01-03    2\n",
      "2017-01-04    3\n",
      "2017-01-05    4\n",
      "2017-01-06    5\n",
      "2017-01-07    6\n",
      "2017-01-08    7\n",
      "2017-01-09    8\n",
      "2017-01-10    9\n",
      "Freq: D, dtype: int32\n"
     ]
    }
   ],
   "source": [
    "import pandas as pd\n",
    "import numpy as np\n",
    "\n",
    "date_rng = pd.date_range('20170101', periods=100, freq='D')\n",
    "ser_obj = pd.Series(range(len(date_rng)), index=date_rng)\n",
    "print(ser_obj.head(10))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "按月求和： 2017-01-31     465\n",
      "2017-02-28    1246\n",
      "2017-03-31    2294\n",
      "2017-04-30     945\n",
      "Freq: M, dtype: int32\n",
      "按月求均值： 2017-01-31    15.0\n",
      "2017-02-28    44.5\n",
      "2017-03-31    74.0\n",
      "2017-04-30    94.5\n",
      "Freq: M, dtype: float64\n"
     ]
    }
   ],
   "source": [
    "# 统计每个月的数据总和\n",
    "resample_month_sum = ser_obj.resample('M').sum()\n",
    "# 统计每个月的数据平均\n",
    "resample_month_mean = ser_obj.resample('M').mean()\n",
    "\n",
    "print('按月求和：', resample_month_sum)\n",
    "print('按月求均值：', resample_month_mean)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 降采样"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "降采样，sum\n",
      "2017-01-01     10\n",
      "2017-01-06     35\n",
      "2017-01-11     60\n",
      "2017-01-16     85\n",
      "2017-01-21    110\n",
      "2017-01-26    135\n",
      "2017-01-31    160\n",
      "2017-02-05    185\n",
      "2017-02-10    210\n",
      "2017-02-15    235\n",
      "2017-02-20    260\n",
      "2017-02-25    285\n",
      "2017-03-02    310\n",
      "2017-03-07    335\n",
      "2017-03-12    360\n",
      "2017-03-17    385\n",
      "2017-03-22    410\n",
      "2017-03-27    435\n",
      "2017-04-01    460\n",
      "2017-04-06    485\n",
      "Freq: 5D, dtype: int32\n"
     ]
    }
   ],
   "source": [
    "# 将数据聚合到5天的频率\n",
    "five_day_sum_sample = ser_obj.resample('5D').sum()\n",
    "five_day_mean_sample = ser_obj.resample('5D').mean()\n",
    "five_day_ohlc_sample = ser_obj.resample('5D').ohlc()\n",
    "\n",
    "print('降采样，sum')\n",
    "print(five_day_sum_sample)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "降采样，mean\n",
      "2017-01-01     2\n",
      "2017-01-06     7\n",
      "2017-01-11    12\n",
      "2017-01-16    17\n",
      "2017-01-21    22\n",
      "2017-01-26    27\n",
      "2017-01-31    32\n",
      "2017-02-05    37\n",
      "2017-02-10    42\n",
      "2017-02-15    47\n",
      "2017-02-20    52\n",
      "2017-02-25    57\n",
      "2017-03-02    62\n",
      "2017-03-07    67\n",
      "2017-03-12    72\n",
      "2017-03-17    77\n",
      "2017-03-22    82\n",
      "2017-03-27    87\n",
      "2017-04-01    92\n",
      "2017-04-06    97\n",
      "Freq: 5D, dtype: int32\n"
     ]
    }
   ],
   "source": [
    "print('降采样，mean')\n",
    "print(five_day_mean_sample)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "降采样，ohlc\n",
      "            open  high  low  close\n",
      "2017-01-01     0     4    0      4\n",
      "2017-01-06     5     9    5      9\n",
      "2017-01-11    10    14   10     14\n",
      "2017-01-16    15    19   15     19\n",
      "2017-01-21    20    24   20     24\n",
      "2017-01-26    25    29   25     29\n",
      "2017-01-31    30    34   30     34\n",
      "2017-02-05    35    39   35     39\n",
      "2017-02-10    40    44   40     44\n",
      "2017-02-15    45    49   45     49\n",
      "2017-02-20    50    54   50     54\n",
      "2017-02-25    55    59   55     59\n",
      "2017-03-02    60    64   60     64\n",
      "2017-03-07    65    69   65     69\n",
      "2017-03-12    70    74   70     74\n",
      "2017-03-17    75    79   75     79\n",
      "2017-03-22    80    84   80     84\n",
      "2017-03-27    85    89   85     89\n",
      "2017-04-01    90    94   90     94\n",
      "2017-04-06    95    99   95     99\n"
     ]
    }
   ],
   "source": [
    "print('降采样，ohlc')\n",
    "print(five_day_ohlc_sample)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1     465\n",
      "2    1246\n",
      "3    2294\n",
      "4     945\n",
      "dtype: int32\n"
     ]
    }
   ],
   "source": [
    "# 使用groupby降采样\n",
    "print(ser_obj.groupby(lambda x: x.month).sum())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0    750\n",
      "1    665\n",
      "2    679\n",
      "3    693\n",
      "4    707\n",
      "5    721\n",
      "6    735\n",
      "dtype: int32\n"
     ]
    }
   ],
   "source": [
    "print(ser_obj.groupby(lambda x: x.weekday).sum())"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 升采样"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "                  S1        S2        S3\n",
      "2017-01-02 -1.144930  2.056606  1.280437\n",
      "2017-01-09  0.665305 -1.167066  0.652643\n",
      "2017-01-16  1.725148  0.310244  0.577190\n",
      "2017-01-23 -0.155053 -0.563262  0.588269\n",
      "2017-01-30  0.352137 -0.223055  0.638517\n"
     ]
    }
   ],
   "source": [
    "df = pd.DataFrame(np.random.randn(5, 3),\n",
    "                 index=pd.date_range('20170101', periods=5, freq='W-MON'),\n",
    "                 columns=['S1', 'S2', 'S3'])\n",
    "print(df)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "                  S1        S2        S3\n",
      "2017-01-02 -1.144930  2.056606  1.280437\n",
      "2017-01-03       NaN       NaN       NaN\n",
      "2017-01-04       NaN       NaN       NaN\n",
      "2017-01-05       NaN       NaN       NaN\n",
      "2017-01-06       NaN       NaN       NaN\n",
      "2017-01-07       NaN       NaN       NaN\n",
      "2017-01-08       NaN       NaN       NaN\n",
      "2017-01-09  0.665305 -1.167066  0.652643\n",
      "2017-01-10       NaN       NaN       NaN\n",
      "2017-01-11       NaN       NaN       NaN\n",
      "2017-01-12       NaN       NaN       NaN\n",
      "2017-01-13       NaN       NaN       NaN\n",
      "2017-01-14       NaN       NaN       NaN\n",
      "2017-01-15       NaN       NaN       NaN\n",
      "2017-01-16  1.725148  0.310244  0.577190\n",
      "2017-01-17       NaN       NaN       NaN\n",
      "2017-01-18       NaN       NaN       NaN\n",
      "2017-01-19       NaN       NaN       NaN\n",
      "2017-01-20       NaN       NaN       NaN\n",
      "2017-01-21       NaN       NaN       NaN\n",
      "2017-01-22       NaN       NaN       NaN\n",
      "2017-01-23 -0.155053 -0.563262  0.588269\n",
      "2017-01-24       NaN       NaN       NaN\n",
      "2017-01-25       NaN       NaN       NaN\n",
      "2017-01-26       NaN       NaN       NaN\n",
      "2017-01-27       NaN       NaN       NaN\n",
      "2017-01-28       NaN       NaN       NaN\n",
      "2017-01-29       NaN       NaN       NaN\n",
      "2017-01-30  0.352137 -0.223055  0.638517\n"
     ]
    }
   ],
   "source": [
    "# 直接重采样会产生空值\n",
    "print(df.resample('D').asfreq())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "                  S1        S2        S3\n",
      "2017-01-02 -1.144930  2.056606  1.280437\n",
      "2017-01-03 -1.144930  2.056606  1.280437\n",
      "2017-01-04 -1.144930  2.056606  1.280437\n",
      "2017-01-05       NaN       NaN       NaN\n",
      "2017-01-06       NaN       NaN       NaN\n",
      "2017-01-07       NaN       NaN       NaN\n",
      "2017-01-08       NaN       NaN       NaN\n",
      "2017-01-09  0.665305 -1.167066  0.652643\n",
      "2017-01-10  0.665305 -1.167066  0.652643\n",
      "2017-01-11  0.665305 -1.167066  0.652643\n",
      "2017-01-12       NaN       NaN       NaN\n",
      "2017-01-13       NaN       NaN       NaN\n",
      "2017-01-14       NaN       NaN       NaN\n",
      "2017-01-15       NaN       NaN       NaN\n",
      "2017-01-16  1.725148  0.310244  0.577190\n",
      "2017-01-17  1.725148  0.310244  0.577190\n",
      "2017-01-18  1.725148  0.310244  0.577190\n",
      "2017-01-19       NaN       NaN       NaN\n",
      "2017-01-20       NaN       NaN       NaN\n",
      "2017-01-21       NaN       NaN       NaN\n",
      "2017-01-22       NaN       NaN       NaN\n",
      "2017-01-23 -0.155053 -0.563262  0.588269\n",
      "2017-01-24 -0.155053 -0.563262  0.588269\n",
      "2017-01-25 -0.155053 -0.563262  0.588269\n",
      "2017-01-26       NaN       NaN       NaN\n",
      "2017-01-27       NaN       NaN       NaN\n",
      "2017-01-28       NaN       NaN       NaN\n",
      "2017-01-29       NaN       NaN       NaN\n",
      "2017-01-30  0.352137 -0.223055  0.638517\n"
     ]
    }
   ],
   "source": [
    "#ffill\n",
    "print(df.resample('D').ffill(2))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "                  S1        S2        S3\n",
      "2017-01-02 -1.144930  2.056606  1.280437\n",
      "2017-01-03  0.665305 -1.167066  0.652643\n",
      "2017-01-04  0.665305 -1.167066  0.652643\n",
      "2017-01-05  0.665305 -1.167066  0.652643\n",
      "2017-01-06  0.665305 -1.167066  0.652643\n",
      "2017-01-07  0.665305 -1.167066  0.652643\n",
      "2017-01-08  0.665305 -1.167066  0.652643\n",
      "2017-01-09  0.665305 -1.167066  0.652643\n",
      "2017-01-10  1.725148  0.310244  0.577190\n",
      "2017-01-11  1.725148  0.310244  0.577190\n",
      "2017-01-12  1.725148  0.310244  0.577190\n",
      "2017-01-13  1.725148  0.310244  0.577190\n",
      "2017-01-14  1.725148  0.310244  0.577190\n",
      "2017-01-15  1.725148  0.310244  0.577190\n",
      "2017-01-16  1.725148  0.310244  0.577190\n",
      "2017-01-17 -0.155053 -0.563262  0.588269\n",
      "2017-01-18 -0.155053 -0.563262  0.588269\n",
      "2017-01-19 -0.155053 -0.563262  0.588269\n",
      "2017-01-20 -0.155053 -0.563262  0.588269\n",
      "2017-01-21 -0.155053 -0.563262  0.588269\n",
      "2017-01-22 -0.155053 -0.563262  0.588269\n",
      "2017-01-23 -0.155053 -0.563262  0.588269\n",
      "2017-01-24  0.352137 -0.223055  0.638517\n",
      "2017-01-25  0.352137 -0.223055  0.638517\n",
      "2017-01-26  0.352137 -0.223055  0.638517\n",
      "2017-01-27  0.352137 -0.223055  0.638517\n",
      "2017-01-28  0.352137 -0.223055  0.638517\n",
      "2017-01-29  0.352137 -0.223055  0.638517\n",
      "2017-01-30  0.352137 -0.223055  0.638517\n"
     ]
    }
   ],
   "source": [
    "print(df.resample('D').bfill())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "                  S1        S2        S3\n",
      "2017-01-02 -1.144930  2.056606  1.280437\n",
      "2017-01-03 -1.144930  2.056606  1.280437\n",
      "2017-01-04 -1.144930  2.056606  1.280437\n",
      "2017-01-05 -1.144930  2.056606  1.280437\n",
      "2017-01-06 -1.144930  2.056606  1.280437\n",
      "2017-01-07 -1.144930  2.056606  1.280437\n",
      "2017-01-08 -1.144930  2.056606  1.280437\n",
      "2017-01-09  0.665305 -1.167066  0.652643\n",
      "2017-01-10  0.665305 -1.167066  0.652643\n",
      "2017-01-11  0.665305 -1.167066  0.652643\n",
      "2017-01-12  0.665305 -1.167066  0.652643\n",
      "2017-01-13  0.665305 -1.167066  0.652643\n",
      "2017-01-14  0.665305 -1.167066  0.652643\n",
      "2017-01-15  0.665305 -1.167066  0.652643\n",
      "2017-01-16  1.725148  0.310244  0.577190\n",
      "2017-01-17  1.725148  0.310244  0.577190\n",
      "2017-01-18  1.725148  0.310244  0.577190\n",
      "2017-01-19  1.725148  0.310244  0.577190\n",
      "2017-01-20  1.725148  0.310244  0.577190\n",
      "2017-01-21  1.725148  0.310244  0.577190\n",
      "2017-01-22  1.725148  0.310244  0.577190\n",
      "2017-01-23 -0.155053 -0.563262  0.588269\n",
      "2017-01-24 -0.155053 -0.563262  0.588269\n",
      "2017-01-25 -0.155053 -0.563262  0.588269\n",
      "2017-01-26 -0.155053 -0.563262  0.588269\n",
      "2017-01-27 -0.155053 -0.563262  0.588269\n",
      "2017-01-28 -0.155053 -0.563262  0.588269\n",
      "2017-01-29 -0.155053 -0.563262  0.588269\n",
      "2017-01-30  0.352137 -0.223055  0.638517\n"
     ]
    }
   ],
   "source": [
    "print(df.resample('D').fillna('ffill'))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "                  S1        S2        S3\n",
      "2017-01-02 -1.144930  2.056606  1.280437\n",
      "2017-01-03 -0.886325  1.596082  1.190753\n",
      "2017-01-04 -0.627720  1.135557  1.101068\n",
      "2017-01-05 -0.369115  0.675032  1.011383\n",
      "2017-01-06 -0.110510  0.214508  0.921698\n",
      "2017-01-07  0.148095 -0.246017  0.832013\n",
      "2017-01-08  0.406700 -0.706542  0.742328\n",
      "2017-01-09  0.665305 -1.167066  0.652643\n",
      "2017-01-10  0.816711 -0.956022  0.641864\n",
      "2017-01-11  0.968118 -0.744978  0.631085\n",
      "2017-01-12  1.119524 -0.533933  0.620306\n",
      "2017-01-13  1.270930 -0.322889  0.609527\n",
      "2017-01-14  1.422336 -0.111845  0.598748\n",
      "2017-01-15  1.573742  0.099200  0.587969\n",
      "2017-01-16  1.725148  0.310244  0.577190\n",
      "2017-01-17  1.456548  0.185457  0.578773\n",
      "2017-01-18  1.187948  0.060671  0.580355\n",
      "2017-01-19  0.919347 -0.064116  0.581938\n",
      "2017-01-20  0.650747 -0.188903  0.583521\n",
      "2017-01-21  0.382147 -0.313689  0.585104\n",
      "2017-01-22  0.113547 -0.438476  0.586686\n",
      "2017-01-23 -0.155053 -0.563262  0.588269\n",
      "2017-01-24 -0.082597 -0.514661  0.595447\n",
      "2017-01-25 -0.010142 -0.466060  0.602626\n",
      "2017-01-26  0.062314 -0.417459  0.609804\n",
      "2017-01-27  0.134770 -0.368858  0.616982\n",
      "2017-01-28  0.207226 -0.320257  0.624160\n",
      "2017-01-29  0.279682 -0.271656  0.631338\n",
      "2017-01-30  0.352137 -0.223055  0.638517\n"
     ]
    }
   ],
   "source": [
    "print(df.resample('D').interpolate('linear'))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "anaconda-cloud": {},
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.5.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}
